mysql = require 'mysql'
config = require '../config'
pool = mysql.createPool config.db

module.exports = 
	runSql:()->
		args = Array.prototype.slice.call arguments
		callback = args.pop()
		pool.getConnection (err,conn)->
			done = (err,result)->
				conn.release()
				callback.apply this,arguments
			args.push done
			conn.query.apply conn, args

	queryOne:()->
		args = Array.prototype.slice.call arguments
		callback = args.pop()
		pool.getConnection (err,conn)->
			done = (err,result)->
				conn.release()
				callback.apply this,[err,result?[0]]
			args.push done
			conn.query.apply conn, args

# if require.main is module
# 	test = module.exports
# 	async = require 'async'
# 	async.forEach [1..10000],(i,cb)->
# 		test.runSql "select 1 ", (err,result)->
# 			console.log result
# 		cb null
